#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<string>
#include<queue>
using namespace std;

int findMaxFish(vector<vector<int>>& grid) {
	int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
	int m = grid.size(), n = grid[0].size();
	vector<vector<bool>> visited(m, vector<bool>(n, false));
	int ret = INT_MIN;
	auto bfs = [&](int i, int j)
	{
		queue<pair<int, int>> q;
		q.push({ i,j });
		visited[i][j] = true;
		int path = grid[i][j];

		while (!q.empty())
		{
			auto [x1, y1] = q.front();
			q.pop();
			for (int k = 0; k < 4; k++)
			{
				int x2 = x1 + dx[k], y2 = y1 + dy[k];
				if (x2 >= 0 && x2 < m && y2 >= 0 && y2 < n && visited[x2][y2] == false
					&& grid[x2][y2]>0)
				{
					path += grid[x2][y2];
					visited[x2][y2] = true;
					q.push({ x2,y2 });
				}
			}

		}
		ret = max(ret, path);
	};
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (grid[i][j] > 0 && visited[i][j] == false)
			{
				bfs(i, j);
			}
		}
	}
	return ret == INT_MIN ? 0 : ret;
}
vector<vector<int>> colorBorder(vector<vector<int>>& grid, int row, int col, int color) {
	int dx[4] = { 0,0,-1,1 }, dy[4] = { 1,-1,0,0 };
	int m = grid.size(), n = grid[0].size();
	if (m == 0 || n == 0)
		return {};
	vector<vector<bool>> visited(m, vector<bool>(n, false));
	vector<vector<int>> ret = grid;
	auto bfs = [&](int i, int j,int precolor)
	{
		queue<pair<int, int>> q;
		q.push({ i,j });
		visited[i][j] = true;
		while (!q.empty())
		{
			auto [x1, y1] = q.front();
			q.pop();
			if (x1 == 0 || x1 == m - 1 || y1 == 0 || y1 == n - 1 ||
				grid[x1 - 1][y1] != precolor || grid[x1 + 1][y1] != precolor ||
				grid[x1][y1 - 1] != precolor || grid[x1][y1 + 1] != precolor)
			{
				ret[x1][y1] = color;
			}
			for (int k = 0; k < 4; k++)
			{
				int x2 = x1 + dx[k], y2 = y1 + dy[k];
				if (x2 >= 0 && x2 < m && y2 >= 0 && y2 < n && visited[x2][y2] == false
					&& grid[x2][y2] == precolor)
				{
					visited[x2][y2] = true;
					q.push({ x2,y2 });
				}
			}
		}
	};
	bfs(row, col, grid[row][col]);
	return ret;
}
int main()
{
	vector<vector<int>> grid = { {8,6},{2,6} };
	findMaxFish(grid);
	return 0;
}